package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.widget.swipelayout.SimpleSwipeListener;
import com.hjq.widget.swipelayout.SwipeLayout;
import com.hjq.widget.swipelayout.adapters.RecyclerSwipeAdapter;
import com.qiantu.api.entity.KGSwitchBindBean;
import com.qiantu.api.entity.SwitchBindBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.KGUISwitchBindBean;
import com.qiantu.phone.bean.UISwitchBindBean;
import com.qiantu.phone.ui.activity.DeviceSettingActivity;
import com.qiantu.phone.ui.activity.KGPanelBindInfoActivity;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.utils.SceneDetailUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class KGSwitchBindAdapter extends AppAdapter<KGUISwitchBindBean> {

    private KGPanelBindInfoActivity activity;

    public KGSwitchBindAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public KGSwitchBindAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new KGSwitchBindAdapter.ViewHolder();
    }


    public final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private final TextView switchBindTitle;
        private final TextView deviceName;
        private final RecyclerView itemListView;
        private KGSwitchBindAdapter.SwitchItemAdapter itemAdapter;

        private ViewHolder() {
            super(R.layout.item_switch_bind);
            switchBindTitle = findViewById(R.id.switch_bind_item_title);
            itemListView = findViewById(R.id.switch_list_view);
            deviceName = findViewById(R.id.tv_device_name);
        }

        @Override
        public void onBindView(int position) {
            KGUISwitchBindBean item = getItem(position);
            itemAdapter = new KGSwitchBindAdapter.SwitchItemAdapter(getContext(), item.getSwitchBindBeans());
            itemListView.setAdapter(itemAdapter);
            itemListView.setLayoutManager(new LinearLayoutManager(getContext()));
            KGSwitchBindBean bean =  item.getSwitchBindBeans().get(0);
            switchBindTitle.setText(bean.getActionType() + " " + bean.getBindActionType());
            deviceName.setText(bean.getBindActionName());
            deviceName.setVisibility(View.VISIBLE);
        }
    }

    public static final class SwitchItemAdapter extends RecyclerSwipeAdapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new KGSwitchBindAdapter.SwitchItemAdapter.ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_switch_bind_sub, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            KGSwitchBindAdapter.SwitchItemAdapter.ItemViewHolder vh = (KGSwitchBindAdapter.SwitchItemAdapter.ItemViewHolder) viewHolder;
            KGSwitchBindBean item = dataList.get(position);
            vh.tvContentTitle.setText(item.getSwitchName() + " " + item.getSwitchKey());
            vh.tvContentDescription.setText(item.getSwicthActionName());
            vh.itemView.setTag(item);
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        @Override
        public int getSwipeLayoutResourceId(int position) {
            return R.id.swipe_layout;
        }

        private final Context context;
        private final List<KGSwitchBindBean> dataList;

        public SwitchItemAdapter(Context context, List<KGSwitchBindBean> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {

            public SwipeLayout swipeLayout;
            public TextView tvContentTitle;
            public TextView tvContentDescription;

            public ItemViewHolder(@NonNull @NotNull View itemView) {
                super(itemView);
                swipeLayout = itemView.findViewById(R.id.swipe_layout);
                tvContentTitle = itemView.findViewById(R.id.tv_content_title);
                tvContentDescription = itemView.findViewById(R.id.tv_content_description);
            }
        }
    }
}
